package com.ruoyi.flowable.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.flowable.domain.WfDeployTaskCopy;
import com.ruoyi.flowable.domain.WfModelTaskCopy;
import com.ruoyi.flowable.mapper.WfDeployTaskCopyMapper;
import com.ruoyi.flowable.service.IWfDeployTaskCopyService;
import com.ruoyi.flowable.service.IWfModelTaskCopyService;
import org.flowable.bpmn.model.BpmnModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 流程实例节点抄送对象Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-31
 */
@Service
public class WfDeployTaskCopyServiceImpl extends ServiceImpl<WfDeployTaskCopyMapper, WfDeployTaskCopy> implements IWfDeployTaskCopyService {

    @Autowired
    private IWfModelTaskCopyService wfModelTaskCopyService;
    /**
     * 保存节点抄送对象
     *
     * @param deployId 部署ID
     * @param modelId  模型ID
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void create(String deployId, BpmnModel bpmnModel, String modelId) {
        List<WfModelTaskCopy> wfModelTaskCopies = wfModelTaskCopyService.selectByModelId(modelId);
        if (CollUtil.isNotEmpty(wfModelTaskCopies)) {
            WfDeployTaskCopy taskCopy = null;
            for (WfModelTaskCopy bo : wfModelTaskCopies) {
                taskCopy = new WfDeployTaskCopy();
                taskCopy.setDeployId(deployId);
                taskCopy.setNodeKey(bo.getNodeKey());
                taskCopy.setNodeName(bo.getNodeName());
                taskCopy.setCopyList(bo.getCopyList());
                save(taskCopy);
            }
        }
    }

    @Override
    public List<WfDeployTaskCopy> selectByDeploy(String modelId) {
        LambdaQueryWrapper<WfDeployTaskCopy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WfDeployTaskCopy::getDeployId, modelId);
        return list(wrapper);
    }

    /**
     * 通过部署ID和节点ID查找抄送配置
     *
     * @param deploymentId 部署ID
     * @param activityId   节点ID
     * @return 抄送配置
     */
    @Override
    public WfDeployTaskCopy selectByDeployIdAndNodeKey(String deploymentId, String activityId) {
        LambdaQueryWrapper<WfDeployTaskCopy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WfDeployTaskCopy::getDeployId, deploymentId);
        wrapper.eq(WfDeployTaskCopy::getNodeKey,activityId);
        return getOne(wrapper);
    }
}
